package com.fh.wanandroid.ui.mainpager;

import android.util.Log;

import com.fh.wanandroid.ui.mainpager.bean.BannerData;
import com.fh.wanandroid.ui.mainpager.bean.FeedArticleListData;
import com.fh.wanandroid.net.RetrofitHelper;
import com.fh.wanandroid.utils.NetErrorUtil;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MainPagerPresenter  extends  MainPagerContract.Presenter{

    //分页数
    private int num;

    @Override
    void loadInitData() {

        num = 0;
        mCompositeDisposable.add(RetrofitHelper.createArticleApi().getFeedArticleList(num)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<FeedArticleListData>() {
                    @Override
                    public void accept(FeedArticleListData articleBean) throws Exception {
//                        mView.refreshArticles(articleModel.getItems());
//                        mView.appendData(girlBean.getResults());
                                mView.refreshArticle(articleBean);
                        Log.e("articleBean","------------------articleBean:"+articleBean.getData().toString());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable t) throws Exception {
//                        mView.loadArticlesFail(NetErrorUtil.handleThrowable(t));
                        mView.loadDataFail(NetErrorUtil.handleThrowable(t));
                    }
                }));


    }

    @Override
    void loadMoreData() {


        mCompositeDisposable.add(RetrofitHelper.createArticleApi().getFeedArticleList(++num)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<FeedArticleListData>() {
                    @Override
                    public void accept(FeedArticleListData articleBean) throws Exception {
//                        mView.refreshArticles(articleModel.getItems());
//                        mView.appendData(girlBean.getResults());
                        mView.appendArticle(articleBean);
                        Log.e("articleBean","------------------articleBean:"+articleBean.getData().toString());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable t) throws Exception {
//                        mView.loadArticlesFail(NetErrorUtil.handleThrowable(t));
                        mView.loadDataFail(NetErrorUtil.handleThrowable(t));
                    }
                }));

    }

    @Override
    void getBannerData() {


        mCompositeDisposable.add(RetrofitHelper.createArticleApi().getBannerData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BannerData>() {
                    @Override
                    public void accept(BannerData bannerData) throws Exception {
//                        mView.refreshArticles(articleModel.getItems());
//                        mView.appendData(girlBean.getResults());
                        mView.getBanner(bannerData);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable t) throws Exception {
//                        mView.loadArticlesFail(NetErrorUtil.handleThrowable(t));
                        mView.loadDataFail(NetErrorUtil.handleThrowable(t));
                    }
                }));

    }
}
